package leecode;

/**
 * 461. 汉明距离
 * easy
 */
public class _461 {
    public static void main(String[] args) {
        int s = hammingDistance(93, 73);
        System.out.println(s);
    }

    public static int hammingDistance(int x, int y) {
        String p1 = Integer.toBinaryString(x);
        String p2 = Integer.toBinaryString(y);
        int min = Math.min(p1.length(), p2.length());
        int left = Math.abs(p1.length() - p2.length());
        int res = 0;
        for (int i = 0; i < left; i++) {
            if (p1.length() > p2.length()) {
                if (p1.charAt(i) == '1') {
                    res++;
                }
            } else {
                if (p2.charAt(i) == '1') {
                    res++;
                }
            }
        }
        for (int i = 0; i < min; i++) {
            if (p1.length() > p2.length()) {
                if (p1.charAt(i + left) != p2.charAt(i)) {
                    res++;
                }
            } else if (p1.length() < p2.length()) {
                if (p1.charAt(i) != p2.charAt(i + left)) {
                    res++;
                }
            } else {
                if (p1.charAt(i) != p2.charAt(i)) {
                    res++;
                }
            }
        }
        return res;
    }

    public int best(int x, int y) {
        return Integer.bitCount(x^y);
    }
}
